using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Minigolf.Gamestates;
using System.IO;
using System.Xml.Serialization;

namespace Minigolf
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Map
    {
        public List<String> walltexture;
        public List<Vector2> wallpos;
        public List<Vector2> terrain;
    }

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager graphics;
        public static StateManager mgrState;
        public static Map map;
        public SpriteBatch spriteBatch;
        public static CTManager content;
        public static Texture2D ShapeRectangle;
        public static List<Map> maps;


        private void LoadMap(Stream streamread, int point)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(string));
            string tempstring0002 = (string)serializer.Deserialize(streamread);
            string[] words = tempstring0002.Split('\n');
            //map.terrain.RemoveRange(0, map.terrain.Count);
            //map.wallpos.RemoveRange(0, map.wallpos.Count);
            //map.walltexture.RemoveRange(0, map.walltexture.Count);
            foreach (string word in words)
            {
                if (word.Contains("walltexture"))
                {
                    string tempstring0010 = word.Replace("walltexture", "");
                    string tempstring0011 = tempstring0010.Replace(":", "");
                    maps[point].walltexture.Add(Convert.ToString(tempstring0011));
                }
                else
                {
                    string tempstring0003 = word.Replace("{X:", "");
                    string tempstring0004 = tempstring0003.Replace(" Y:", "|");
                    string tempstring0005 = tempstring0004.Replace("}", "");
                    if (word.Contains("terrain"))
                    {
                        string tempstring0006 = tempstring0005.Replace("terrain", "");
                        maps[point].terrain.Add(new Vector2(Int32.Parse(tempstring0006.Split('|')[0]), Int32.Parse(tempstring0006.Split('|')[1])));
                    }
                    if (word.Contains("wallpos"))
                    {
                        string tempstring0007 = tempstring0005.Replace("wallpos", "");
                        maps[point].wallpos.Add(new Vector2(Int32.Parse(tempstring0007.Split('|')[0]), Int32.Parse(tempstring0007.Split('|')[1])));
                    }
                }
            }
        }

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            IsMouseVisible = true;
            Window.Title = "Minigolf";

            content = new CTManager(Services, "Content");
        }


        public static int ScrW()
        {
            return graphics.PreferredBackBufferWidth;
        }

        public static int ScrH()
        {
            return graphics.PreferredBackBufferWidth;
        }

        protected override void Initialize()
        {
            for (int I = 1; I < 9; I++)
            {
                if (File.Exists("level" + I + ".xml"))
                {
                    LoadMap(File.OpenRead("level" + I + ".xml"), I);
                }
            }

            base.Initialize();

            mgrState = new StateManager(this);

            mgrState.Set<SinglePlayer>(); // Default Menu is Main Menu
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Texture2D Rect = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            Rect.SetData(new[] { Color.White });
            ShapeRectangle = Rect;
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            mgrState.ActiveState.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            mgrState.ActiveState.Render(RenderStage.PreWorld, spriteBatch);
            //
            mgrState.ActiveState.Render(RenderStage.PostWorld, spriteBatch);

            mgrState.ActiveState.Render(RenderStage.PreGui, spriteBatch);
            //
            mgrState.ActiveState.Render(RenderStage.PostGui, spriteBatch);

            base.Draw(gameTime);
        }
    }
}
